define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
	let tableInstance = null; // 表格实例

	const controller = {
		index() {
			// 初始化
			Table.api.init();

			// 绑定事件
			$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
				var panel = $($(this).attr('href'));
				if (panel.length > 0) {
					controller.table[panel.attr('id')].call(this);
					$(this).on('click', function (e) {
						$($(this).attr('href')).find('.btn-refresh').trigger('click');
					});
				}

				//移除绑定的事件
				$(this).unbind('shown.bs.tab');
			});

			//必须默认触发shown.bs.tab事件
			$('ul.nav-tabs li.active a[data-toggle="tab"]').trigger('shown.bs.tab');

			// 点击还原回调
			$('.btn-restore').click(() => {
				const ids = Table.api.selectedids(tableInstance);

				layer.confirm(
					'确定恢复吗？',
					{
						title: '提示',
						btn: ['确定', '取消'],
					},
					(index) => {
						Backend.api.ajax(
							{
								url: $.fn.bootstrapTable.defaults.extend.restore_url + '?ids=' + ids,
							},
							() => {
								//关闭弹框
								layer.close(index);

								//刷新表格
								tableInstance.bootstrapTable('refresh');
							},
							(err, res) => {
								console.log(err, res);
								layer.close(index);
							}
						);
					}
				);
			});

			// 点击销毁回调
			$('.btn-destroy').click(() => {
				const ids = Table.api.selectedids(tableInstance);

				layer.confirm(
					'确定销毁吗？',
					{
						title: '提示',
						btn: ['确定', '取消'],
					},
					(index) => {
						Backend.api.ajax(
							{
								url: $.fn.bootstrapTable.defaults.extend.destroy_url + '?ids=' + ids,
							},
							() => {
								//关闭弹框
								layer.close(index);

								//刷新表格
								tableInstance.bootstrapTable('refresh');
							},
							(err, res) => {
								console.log(err, res);
								layer.close(index);
							}
						);
					}
				);
			});
		},
		table: {
			product() {
				Table.api.init({
					extend: {
						recyclebin_url: 'product/product/recyclebin',
						restore_url: 'product/product/restore',
						destroy_url: 'product/product/destroy',
						table: 'product',
					},
				});

				const table = $('#table1');

				table.bootstrapTable({
					// 请求地址
					url: $.fn.bootstrapTable.defaults.extend.recyclebin_url,
					// 设置工具栏元素
					toolbar: '#toolbar1',
					// 设置主键
					pk: 'id',
					// 排序字段
					sortName: 'createtime',
					// 默认排序方式
					sortOrder: 'desc',
					showExport: false,
					fixedColumns: true,
					fixedRightNumber: 1,
					// 渲染表格数据
					columns: [
						{ checkbox: true },
						{ field: 'id', title: __('Id'), sortable: true },
						{ field: 'name', title: __('Name'), operate: 'LIKE' },
						{
							field: 'status',
							title: __('Status'),
							searchList: { 0: __('下架'), 1: __('上架') },
							formatter: Table.api.formatter.flag,
						},
						{
							field: 'flag',
							title: __('Flag'),
							searchList: { 1: __('新品'), 2: __('热销'), 3: __('推荐') },
							formatter: Table.api.formatter.flag,
						},
						{ field: 'stock', title: __('Stock') },
						{ field: 'price', title: __('Price') },
						{ field: 'type.name', title: __('Typeid') },
						{ field: 'unit.name', title: __('Unitid') },
						{
							field: 'createtime',
							title: __('Createtime'),
							operate: 'RANGE',
							addclass: 'datetimerange',
							autocomplete: false,
							formatter: Table.api.formatter.datetime,
						},
						{
							field: 'operate',
							title: __('Operate'),
							table: table,
							events: Table.api.events.operate,
							formatter: Table.api.formatter.operate,
							buttons: [
								{
									name: 'restore',
									title: '恢复',
									classname: 'btn btn-xs btn-success btn-ajax',
									extend: "data-toggle='tooltip'",
									icon: 'fa fa-reply',
									confirm: '确认恢复？',
									url: $.fn.bootstrapTable.defaults.extend.restore_url,
									success(data, ret) {
										Layer.alert(ret.msg);
										// 刷新表格
										table.bootstrapTable('refresh');
									},
									error(data, ret) {
										console.log(data, ret);
										Layer.alert(ret.msg);
										return false;
									},
								},
								{
									name: 'destroy',
									title: '销毁',
									classname: 'btn btn-xs btn-danger btn-ajax',
									extend: "data-toggle='tooltip'",
									icon: 'fa fa-trash',
									confirm: '确认销毁？',
									url: $.fn.bootstrapTable.defaults.extend.destroy_url,
									success() {
										// 刷新表格
										table.bootstrapTable('refresh');
									},
									error(data, ret) {
										console.log(data, ret);
										Layer.alert(ret.msg);
										return false;
									},
								},
							],
						},
					],
				});

				Table.api.bindevent(table);
				tableInstance = table;
			},
			order() {
				Table.api.init({
					extend: {
						recyclebin_url: 'product/order/recyclebin',
						destroy_url: 'product/order/destroy',
						restore_url: 'product/order/restore',
						table: 'order',
					},
				});

				const table = $('#table2');

				// 初始化表格
				table.bootstrapTable({
					url: $.fn.bootstrapTable.defaults.extend.recyclebin_url,
					pk: 'id',
					sortName: 'id',
					toolbar: '#toolbar2',
					columns: [
						[
							{ checkbox: true },
							{ field: 'id', title: __('Id') },
							{
								field: 'code',
								title: __('Code'),
								operate: 'LIKE',
								table: table,
								class: 'autocontent',
								formatter: Table.api.formatter.content,
							},
							{ field: 'business.nickname', title: __('Busid'), operate: 'LIKE' },
							{ field: 'amount', title: __('Amount'), operate: 'BETWEEN' },
							{ field: 'express.name', title: __('Expressid') },
							{
								field: 'expresscode',
								title: __('Expresscode'),
								operate: 'LIKE',
								table: table,
								class: 'autocontent',
								formatter: function (value) {
									if (!value) {
										return '-';
									}

									return `<div class="autocontent-item " style="white-space: nowrap; text-overflow:ellipsis; overflow: hidden; max-width:250px;">${value}</div>`;
								},
							},
							{ field: 'status_text', title: __('Status'), operate: 'LIKE' },
							{
								field: 'createtime',
								title: __('Createtime'),
								operate: 'RANGE',
								addclass: 'datetimerange',
								autocomplete: false,
								formatter: Table.api.formatter.datetime,
							},
							{
								field: 'deletetime',
								title: __('Deletetime'),
								operate: 'RANGE',
								addclass: 'datetimerange',
								autocomplete: false,
								formatter: Table.api.formatter.datetime,
							},
							{
								field: 'operate',
								width: '140px',
								title: __('Operate'),
								table: table,
								events: Table.api.events.operate,
								buttons: [
									{
										name: 'restore',
										title: '恢复',
										classname: 'btn btn-xs btn-success btn-ajax',
										extend: "data-toggle='tooltip'",
										icon: 'fa fa-reply',
										confirm: '确认恢复？',
										url: $.fn.bootstrapTable.defaults.extend.restore_url,
										success(data, ret) {
											Layer.alert(ret.msg);
											// 刷新表格
											table.bootstrapTable('refresh');
										},
										error(data, ret) {
											console.log(data, ret);
											Layer.alert(ret.msg);
											return false;
										},
									},
									{
										name: 'destroy',
										title: '销毁',
										classname: 'btn btn-xs btn-danger btn-ajax',
										extend: "data-toggle='tooltip'",
										icon: 'fa fa-trash',
										confirm: '确认销毁？',
										url: $.fn.bootstrapTable.defaults.extend.destroy_url,
										success() {
											// 刷新表格
											table.bootstrapTable('refresh');
										},
										error(data, ret) {
											console.log(data, ret);
											Layer.alert(ret.msg);
											return false;
										},
									},
								],
								formatter: Table.api.formatter.operate,
							},
						],
					],
				});

				Table.api.bindevent(table);
				tableInstance = table;
			},
		},
		api: {
			// JS模块化的全局方法
			bindevent: function () {
				Form.api.bindevent($('form[role=form]'));
			},
		},
	};

	return controller;
});
